<?php
/**
 * Table schema class
 * @author Artur Kmera <artur.kmera@3code.pl>
 * @version 0.9
 * @package ORM
 * @subpackage Schema
 * @link http://arrowplatform.org/orm
 * @copyright 2011 Arrowplatform 
 * @license GNU LGPL
*/

class Table implements ISchemaElement{
	/**
	 * Class path ( slash separated )
	 * @var string
	 */
	private $classPath;
	
	
	/**
	 * Base class path ( slash separated )
	 * @var string
	 */
	private $baseClassPath;
	
	
	/**
	 * Db table name
	 * @var string
	 */
	private $table;
	
	/**
	 * Table fields ( array )
	 * @var Field []
	 */
	private $fields = array();
	
	/**
	 * Foreign keys
	 * @var ForeignKey []
	 */
	private $foreignKeys = array();
	
	/**
	 * Indexes
	 * @var Index []
	 */
	private $indexes = array();
	
	public function setClasspath($classpath){
		$this->classPath = $classpath;
	}
	
	public function getClasspath(){
		return $this->classPath;
	}
	
	public function setBaseClasspath($classpath){
		$this->baseClassPath = $classpath;
	}
	
	public function getBaseClasspath(){
		return $this->baseClassPath;
	}
	
	/**
	 * Returns  table class name ( Domain object class )
	 * @todo Implement from classpath
	 */
	public function getClass(){
		$baseClass = explode('.',$this->getClasspath());
	    return end($baseClass);
	}
	
	/**
	 * Returns base class 
	 * @todo Implement from baseclasspath
	 */
	public function getBaseClass(){
		$baseClass = explode('.',$this->getBaseClasspath());
	    return end($baseClass);
	}
	//
	
	
	public function setTableName($tableName){
		$this->table = $tableName;
	}
	
	public function getTableName(){
		return $this->table;
	}
	
	public function addForeignKey(ForeignKey $foreignKey){
		$this->foreignKeys[] = $foreignKey;
	}
	
	public function getForeignKeys(){
		return $this->foreignKeys;
	}
	
	public function addIndex(Index $index){
		$this->indexes[] = $index;
	}
	
	/**
	 * Returns table indexes
	 * @return Index []
	 */
	public function getIndexes(){
		return $this->indexes;
	}
	
	
	/**
	 * Returns table fields
	 * @return Field []
	 */
	public function getFields(){
		return $this->fields;
	}
	
	/**
	 * Add field to table
	 * @param Field $field
	 * @todo check that fielf already exists in schema
	 */
	public function addField(Field $field){
		$this->fields[] = $field;
	}
	
	/**
	 * Removes field from fieldlist in table
	 * @param Field $field
	 */
	public function deleteField(Field $field){
		foreach($this->fields as  $key => $field)
			if($field->getName() == $name){
				unset($this->fields[$key]);
				return true;
			}
		throw new SchemaException("Field '{$name}' not exists in table '{$this->table}'");
	}
	
	/**
	 * Return field by given name
	 * @param Field $name
	 */	
	public function getFieldByName($name){
		foreach($this->fields as $field)
			if($field->getName() == $name)
				return $field;
		throw new SchemaException("Field '{$name}' not exists in table '{$this->table}'");
	}

	/**
	 * @todo implement
	 */
	public function getIndexByName(){}
	

	/**
	 * (non-PHPdoc)
	 * @see ISchemaElement::toString()
	 */
	public function toString(){
		return "Type: Table, Class: {$this->getClass()}, Table: {$this->getTableName()}";
	}

	/**
	 * (non-PHPdoc)
	 * @see ISchemaElement::toArray()
	 */
	public function toArray(){
		$fields = array();
		
		foreach($this->fields as $key => $field){
			$fields[$key] = $field->toArray();
		}
		
		return array(
			"class" => $this->class,
			"classPath" => $this->classPath,
			"baseClass" => $this->baseClass,
			"baseClassPath" => $this->baseClassPath,
			"table" => $this->table,
			"fields" => $fields,
			"indexes" => $this->indexes,
			"foreignKeys" => $this->foreignKeys,
			
		);
	}
}
?>